package com.zoo.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoo.entity.User;
import com.zoo.service.impl.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  用户控制器
 * </p>
 *
 * @author Owen
 * @since 2020-10-11
 */
@RestController
@RequestMapping(value = "/api/")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceImpl userService;

    /**
     * 用户信息保存
     * @param user
     * @return
     */
    @PostMapping("/save-user")
    public String saveUser(@RequestBody User user) {
        user.setId("001");
        user.setMobile("187919180925");
        try {
            userService.save(user);
        } catch (Exception e) {
            logger.error("保存用户失败: {}", e);
            return "save fail";
        }
        return "save success";
    }


    /**
     * 用户信息删除
     * @param userId
     * @return
     */
    @PostMapping("/delete-user")
    public String deleteUser(String userId) {
        try {
            userService.removeById(userId);
        } catch (Exception e) {
            logger.error("删除用户失败: {}", e);
            return "delete fail";
        }
        return "delete success";
    }


    /**
     * 用户信息修改
     * @param user
     * @return
     */
    @PostMapping("/update-user")
    public String updateUser(@RequestBody User user) {
        try {
            userService.updateById(user);
        } catch (Exception e) {
            logger.error("修改用户失败: {}", e);
            return "update fail";
        }
        return "upadte success";
    }


    /**
     * 用户信息查询，返回一个对象
     * @param userId
     * @return
     */
    @GetMapping("/one-user")
    public User queryOneUser(String userId) {
        return userService.getById(userId);
    }


    /**
     * 用户信息查询
     * @param userId
     * @return
     */
    @GetMapping("/query-user")
    public List<User> queryUser(String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id","001");
        return userService.list(queryWrapper);
    }

}

